$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var clientDatagrid, clientDialog, clientForm, followupDialog, followupForm,transferBillDialog,
        transferBillForm,clientSearchBtn;
    clientDatagrid = $("#client_datagrid");
    clientDialog = $("#client_dialog");
    clientForm = $("#client_form");
    followupDialog = $("#followupBill_dialog");
    followupForm = $("#followupBill_form");
    transferBillDialog = $("#transferBill_dialog");
    transferBillForm = $("#transferBill_form");
    clientSearchBtn=$("#searchBtn");
    /*
     * 初始化数据表格
     */

    /*
     * 初始化新增/编辑对话框
     */
    clientDialog.dialog({
        width: 300,
        height: 580,
        closed: true,
        buttons: '#client_dialog_bt'
    });
    followupDialog.dialog({
        width: 300,
        height: 300,
        closed: true,
        buttons: '#followupBill_dialog_bt'
    });
    transferBillDialog.dialog({
        width: 300,
        height: 300,
        closed: true,
        buttons: '#transferBill_dialog_bt'
    });
    clientSearchBtn.textbox({
        width:230,
        label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
            var keyword = $(this).val();
            clientDatagrid.datagrid("load",{
                keyword:keyword
            });
        }
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        //客户移交
        transfer: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                transferBillForm.form("clear");
                transferBillDialog.dialog("setTitle", "编辑");
                transferBillDialog.dialog("open");
                rowData['client.realname']=rowData.realname;
                rowData['client.id']=rowData.id;
                rowData['transfer.realname']=rowData.manager.realname;
                rowData['transfer.id']=rowData.manager.id;
                transferBillForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要移交的客户!", "warining");
            }
        },
        saveTransferBill: function () {
        var url;
        var idVal = $("[name='id']").val();
        if (idVal) {
            url = "/transferBill/update"
        } else {
            url = "/transferBill/save";
        }
        transferBillForm.form("submit", {
            url: url,
            success: function (data) {
                data = $.parseJSON(data);
                if (data.success) {
                    $.messager.alert("温馨提示", data.msg, "info", function () {
                        transferBillDialog.dialog("close");
                    });
                } else {
                    $.messager.alert("温馨提示", data.msg, "error");
                }
            }
        });
    },
    cancelTransferBill: function () {
        transferBillDialog.dialog("close");
    },
        //客户跟进
        followup: function () {
            //把缓存中所有的数据重新加载allPermission的数据表格中
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                followupForm.form("clear");
                followupDialog.dialog("setTitle", "新增");
                followupDialog.dialog("open");
                rowData['client.id'] = rowData.id;
                followupForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要跟进的客户!", "warining");
            }
        },
        saveBill: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/followupBill/update"
            } else {
                url = "/followupBill/save";
            }
            followupForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            followupDialog.dialog("close");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancelBill: function () {
            followupDialog.dialog("close");
        },
        //放入资源池
        putPool: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要将此客户放入资源池吗？", function (yes) {
                    if (yes) {
                        $.get("/client/putPool?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //转正
        formal: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要将此客户转为正式客户吗？", function (yes) {
                    if (yes) {
                        $.get("/client/formal?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //无效客户
        invalid: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要无效此客户吗？", function (yes) {
                    if (yes) {
                        $.get("/client/invalid?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //增删改,
        add: function () {
            //把缓存中所有的数据重新加载allPermission的数据表格中
            clientForm.form("clear");
            clientDialog.dialog("setTitle", "新增");
            clientDialog.dialog("open");
        },
        edit: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                clientForm.form("clear");
                clientDialog.dialog("setTitle", "编辑");
                clientDialog.dialog("open");
                clientForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要编辑的数据!", "warining");
            }
        },
        del: function () {
            var rowData = clientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要删除选中数据吗？", function (yes) {
                    if (yes) {
                        $.get("/client/delete?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要删除的数据!", "warining");
            }
        },
        //刷新,确认,取消
        reload: function () {
            clientDatagrid.datagrid("reload");
        },
        save: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/client/update"
            } else {
                url = "/client/save";
            }
            clientForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            clientDialog.dialog("close");
                            clientDatagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancel: function () {
            clientDialog.dialog("close");
        }
    }
});

function affirm() {
    var dataRow = $("#client_datagrid").datagrid("getSelected");
    if (dataRow) {
        $("#ob_dialog").dialog("close");
        $("#all_form").form("load",dataRow);

    } else {
        $.messager.alert("温馨提示","请选择一个客户信息","error")
    }
}